package com.pds.web.core.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.pds.common.exception.ServiceException;
import com.pds.common.utils.SecurityUtils;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;
import java.util.Date;

@Component
public class MyBatisPlusHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        this.setFieldValByName("createTime", new Date(), metaObject);
        this.setFieldValByName("entryTime", new Date(), metaObject);
        this.setFieldValByName("updateTime", new Date(), metaObject);
        try {
            this.setFieldValByName("updateBy", SecurityUtils.getUsername(),metaObject);
            this.setFieldValByName("createBy", SecurityUtils.getUsername(),metaObject);
        }
        catch (ServiceException e){
            this.setFieldValByName("updateBy", "SYSTEM",metaObject);
            this.setFieldValByName("createBy", "SYSTEM",metaObject);
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
    }
}
